local PlayerMeta = FindMetaTable("Player")
local EntityMeta = FindMetaTable("Entity")

CPPI = CPPI or {}

CPPI_DEFER = 1024000
CPPI.DEFER = 1024000
CPPI_NOTIMPLEMENTED = 2048000
CPPI.NOTIMPLEMENTED = 2048000

function CPPI.GetName()
	return "KKB"
end

function CPPI.GetVersion()
	return "1.0"
end

function CPPI.GetInterfaceVersion()
	return "1.1"
end

function CPPI.GetNameFromUID( uid )
	return CPPI_NOTIMPLEMENTED
end

function PlayerMeta:CPPIGetFriends()
	local UniqueID = self:UniqueID()
	if ( KKB.BuddyLists[UniqueID] != nil ) then
		return KKB.Buddies[UniqueID]
	else
		return CPPI_DEFER
	end
end

function EntityMeta:CPPIGetOwner()
	if ( self.KKB_OwnerID == nil ) then
		return nil, nil
	elseif ( IsValid(self.KKB_Owner) ) then
		return self.KKB_Owner, self.KKB_OwnerID
	elseif ( not IsValid(self.KKB_Owner) ) then
		return nil, self.KKB_OwnerID
	end
end

--[		Custom Variable Added To CPPISetOwner()		]--
--[		dont_auto_cleanup							]--
--[		pass true to never auto cleanup				]--
--[		entity after player disconnect				]--
--[		Wont let me pass an extra varg				]--
--[		Here is a custom function to do it			]--
function EntityMeta:KKB_SetDontAutoCleanup( dont_auto_cleanup )
	self.KKB_DontAutoCleanup = dont_auto_cleanup
end
function EntityMeta:CPPISetOwner( ply )
	if ( ply ) then
		if ( not IsValid(ply) ) then
			return false
		end
		KKB.SetOwner( self, ply )
		return true
	else
		return false
	end
end

function EntityMeta:CPPISetOwnerUID( uid )
	self.KKB_OwnerID = uid
	return true
end

function EntityMeta:CPPICanTool( ply, ToolName )
	local CanTool = false
	if ( IsValid(ply) ) then
		CanTool = KKB.CanTool( ply, self, ToolName )
	end
	return CanTool
end

function EntityMeta:CPPICanPhysgun( ply )
	if ( IsValid(ply) )  then
		KKB.PhysgunPickup( ply, self )
	end
	return false
end

function EntityMeta:CPPICanPickup( ply )
	if ( IsValid(ply) ) then
		return KKB.PhysgunPickup( ply, self )
	end
	return false
end

function EntityMeta:CPPICanPunt( ply )
	if ( not IsValid(ply) ) then
		return false
	end
	return true
end

local function CPPIInitGM()
	function GAMEMODE:CPPIAssignOwnership(ply, ent)
	end
	function GAMEMODE:CPPIFriendsChanged(ply, ent)
	end
end
hook.Add("Initialize", "CPPIInitGM", CPPIInitGM)